package dao;

import domain.Comments;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import util.JDBCUtils;
import java.util.List;

public class CommentsDaoImpl implements CommentsDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    public List<Comments> getCommentsByCnid(int cnid) {
        String sql = "select * from comments where cnid = ?";
        List<Comments> commentsList = template.query(sql, new BeanPropertyRowMapper<Comments>(Comments.class), cnid);
        return commentsList;
    }

    public int addComments(Comments comment) {
        int cnid = comment.getCnid();
        String ccontent = comment.getCcontent();
        String cdate = comment.getCdate();
        String cip = comment.getCip();
        String cauthor = comment.getCauthor();
        String sql = "insert into comments(cnid,ccontent,cdate,cip,cauthor) values(?,?,?,?,?)";
        return template.update(sql, cnid, ccontent, cdate, cip, cauthor);
    }

    public int deleteComments(int cid, int nid) {
        String sql = "delete from comments where cid = ? and cnid = ?";
        return template.update(sql,cid,nid);
    }
}
